package callnumber.gtdev5.com.photoVault.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.adapter.RecycleAdapter;
import callnumber.gtdev5.com.photoVault.base.BaseFragment;
import callnumber.gtdev5.com.photoVault.bean.PhNameBean;
import callnumber.gtdev5.com.photoVault.constants.AppConstans;
import callnumber.gtdev5.com.photoVault.utils.IsChoose;


public class SettingFragment extends BaseFragment {
    Unbinder unbinder;
    @BindView(R.id.iv_add)
    ImageView iv_add;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.dele)
    ImageView dele;
    @BindView(R.id.head_relative)
    RelativeLayout head_relative;
    private RecycleAdapter adapter;
    private List<String> list = new ArrayList<String>();
    private List<PhNameBean> pName;


    ArrayList<String> mDatas;
    private IsChoose isChoose;
    private static final int COMPLETED = 0;

    @Override
    protected int getLayouId() {
        return R.layout.fragment_setting;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        Log.e("TAG", "SettingFragment+initViews");
    }

    @Override
    protected void initDatas() {
        boolean isFirstLoadTimeData = SpUtils.getInstance().getBoolean(AppConstans.ISFIRSTLOADTIMEDATA, true);
        if (isFirstLoadTimeData) {
            SpUtils.getInstance().putBoolean(AppConstans.ISFIRSTLOADTIMEDATA, false);
        }


    }


    @Override
    protected void initAction() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        mRecyclerView.setLayoutManager(manager);
//      获取数据，向适配器传数据，绑定适配器.
        pName = new ArrayList<>();
        pName = DataSupport.findAll(PhNameBean.class);
        if (pName != null && !pName.isEmpty()) {
            for (PhNameBean phNameBean : pName) {
                list.add(phNameBean.getName());
            }
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        } else {
            list = initData();

        }

        adapter = new RecycleAdapter(getActivity(), list);
        mRecyclerView.setAdapter(adapter);
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert_edit();
            }
        });
        dele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iSSHOW = iSSHOW == GONECHECK ? SHOWCHECK : GONECHECK;
                adapter.setMode(iSSHOW);
            }
        });
    }


    protected ArrayList<String> initData() {
        mDatas = new ArrayList<String>();
        for (int i = 0; i < 4; i++) {
            mDatas.add("默认相册" + (i + 1));
            PhNameBean phNameBean = new PhNameBean();
            phNameBean.setName("默认相册");
            phNameBean.setIsSelect(0);
            phNameBean.setNum((i));
            phNameBean.setId(0);
            phNameBean.save();
        }
        return mDatas;
    }

    String newName;

    public void alert_edit() {
        final EditText et = new EditText(getContext());
        new AlertDialog.Builder(getContext()).setTitle("请输入文件夹名称")
//                .setIcon(android.R.drawable.sym_def_app_icon)
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //按下确定键后的事件
                        for (int n = 0; n < list.size(); n++) {
                            if (et.getText().toString().equals(list.get(n))) {
                                ToastUtils.showShortToast("命名重复！");
                                return;
                            } else if (TextUtils.isEmpty(et.getText().toString())) {
                                ToastUtils.showShortToast("命名不能为空！");
                                return;
                            }
                        }
                        newName = et.getText().toString();
                        adapter.addData(list.size(), newName);
                    }
                }).setNegativeButton("取消", null).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    private int iSSHOW = GONECHECK;
    private static final int GONECHECK = 0;
    private static final int SHOWCHECK = 1;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

//    public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> implements View.OnClickListener {
//        private Context context;
//        private List<String> list;
//        private   ArrayList<IsChoose> MyarrayList;
//        private int VISIBLE = 0;
//        private int ISCHOOSE = 0;
//        private int isshow = 0;
//        private IsChoose isChoose;
//        private int isSelect;
//        private MyViewHolder holder;
//        public RecycleAdapter(Context context, List<String> list) {
//            this.context = context;
//            this.list = list;
//        }
//
//        @Override
//        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//
//            View inflate = LayoutInflater.from(context).inflate(R.layout.item_home, null, true);
//            holder = new MyViewHolder(inflate);
//            holder.mCheckBox.setVisibility(View.GONE);
//            inflate.setOnClickListener(this);
//            return holder;
//        }
//
//        @Override
//        public void onBindViewHolder(MyViewHolder holder, final int position) {
//            holder.tv.setText(list.get(position));
//            isChoose = MyarrayList.get(position);
//            holder.mCheckBox.setVisibility(View.GONE);
//            if (isshow == VISIBLE) {
//                holder.mCheckBox.setVisibility(View.GONE);
//            } else {
//                holder.mCheckBox.setVisibility(View.VISIBLE);
//                isSelect = isChoose.isSelect;
//                if (isSelect != ISCHOOSE) {
//                    holder.mCheckBox.setImageResource(R.mipmap.ic_checked);
//                } else {
//                    holder.mCheckBox.setImageResource(R.mipmap.ic_uncheck);
//                }
//            }
//            holder.photo_one.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    pName = new ArrayList<>();
//                    pName = DataSupport.findAll(PhNameBean.class);
//                    Intent intent = new Intent(context, PhotoActivity.class);
////                    if (pName != null && !pName.isEmpty()) {
//                    intent.putExtra("ids", pName.get(position).getId());
////                    } else {
////                        intent.putExtra("ids", 0);
////                    }
//                    context.startActivity(intent);
//                }
//            });
//            if (pName != null && pName.size() > 0) {
//                switch (pName.get(position).getNum()) {
//                    case 0:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenxiangce1));
//                        break;
//                    case 1:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenxiangce2));
//                        break;
//                    case 2:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenxiangce3));
//                        break;
//                    case 3:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenixangce4));
//                        break;
//
//
//                }
//            } else {
//                switch (position) {
//                    case 0:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenxiangce1));
//                        break;
//                    case 1:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenxiangce2));
//                        break;
//                    case 2:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenxiangce3));
//                        break;
//                    case 3:
//                        holder.photo_one.setBackgroundDrawable(getContext().getResources().getDrawable(R.mipmap.morenixangce4));
//                        break;
//
//
//                }
//            }
//            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (position == 0) {
//                        Snackbar.make(v, "此条目不能删除", Snackbar.LENGTH_SHORT).show();
//                    } else {
//                        //               删除自带默认动画
//                        removeData(position);
//                        DataSupport.deleteAll(FileNameBean.class, "ids = ?", pName.get(position).getId() + "");
//                    }
//                }
//            });
//        }
//        /**
//         *是否显示选择筐
//         */
//        public void setMode(int isshow) {
//            this.isshow = isshow;
//            notifyDataSetChanged();
//        }
//
//        @Override
//        public int getItemCount() {
//            return list.size();
//        }
//
//        int num;
//
//        //  添加数据
//        public void addData(int position) {
////      在list中添加数据，并通知条目加入一条
//            pName = DataSupport.findAll(PhNameBean.class);
//            num = pName.get(position - 1).getNum();
//            list.add(position, newName);
//            //添加动画
//            PhNameBean phNameBean = new PhNameBean();
//            phNameBean.setName(newName);
//            if (num >= 3) {
//                phNameBean.setNum(0);
//                num = 0;
//            } else {
//                num++;
//                phNameBean.setNum(num);
//            }
//            phNameBean.save();
//            pName = DataSupport.findAll(PhNameBean.class);
//            notifyItemInserted(position);
//        }
//
//        //  删除数据
//        public void removeData(int position) {
//            DataSupport.delete(PhNameBean.class, pName.get(position).getId());
//            list.remove(position);
//            //删除动画
//            notifyItemRemoved(position);
//            notifyDataSetChanged();
//        }
//
//        @Override
//        public void onClick(View v) {
//            if (mItemClickListener != null) {
//                mItemClickListener.onItemClick((Integer) v.getTag());
//            }
//        }
//        public OnItemClickListener mItemClickListener;
//        public interface OnItemClickListener {
//            void onItemClick(int position);
//        }
//        /**
//         * ViewHolder的类，用于缓存控件
//         */
//        class MyViewHolder extends RecyclerView.ViewHolder {
//            TextView tv, tv_delete;
//            LinearLayout photo_one;
//            ImageView mCheckBox;
//            //因为删除有可能会删除中间条目，然后会造成角标越界，所以必须整体刷新一下！
//            public MyViewHolder(View view) {
//                super(view);
//                tv = (TextView) view.findViewById(R.id.id_num);
//                tv_delete = (TextView) view.findViewById(R.id.tv_delete);
//                photo_one = view.findViewById(R.id.photo_one);
//                mCheckBox= view.findViewById(R.id.check_box);
//            }
//        }
//    }

}
